const fs = window.require('fs');

export function openharmony_get_report_show_info(path) {
    let general_info_file = path + '/t_general_info.csv';
    let start_time = 0;
    let duration = 0;

    const general_info = fs.readFileSync(general_info_file, 'utf-8');
    let lines = general_info.split(/[(\r\n)\r\n]/);

    for (let line of lines) {
        let key_val = line.split(',');

        if (key_val[0] === 'startTime') {
            start_time = parseInt(key_val[1]);
        }

        if (key_val[0] === 'testDuration') {
            duration = parseInt(key_val[1]);
            break;
        }
    }

    return {start_time, duration};
}

function generate_temp_info_for_report(report_detail) {
    report_detail.fps_target = 60;
    report_detail.cpu_cluster_num = 3;
    report_detail.cpu_core_num = 8;

    let cpu_c0_index = [0, 1, 2, 3];
    let cpu_c1_index = [4, 5, 6];
    let cpu_c2_index = [7];
    report_detail.cpu_index.push(cpu_c0_index);
    report_detail.cpu_index.push(cpu_c1_index);
    report_detail.cpu_index.push(cpu_c2_index);

    for (let i = 0; i < report_detail.cpu_cluster_num; i++) {
        report_detail.cpu_max_freq.push(0);
        report_detail.cpu_min_freq.push(0);
        report_detail.cpu_freq.push([]);
    }

    for (let i = 0; i < report_detail.cpu_core_num; i++) {
        report_detail.cpu_usage.push([]);
        report_detail.cpu_average_usage.push(0);
        report_detail.cpu_high_load.push(0);
    }

    report_detail.gpu_max_freq = 0;
    report_detail.gpu_min_freq = 0;
}

let period_data_map = {
    fps: -1,
    fps_frame_time: -1,

    cpu_cluster_freq: [],
    cpu_usage: [],

    gpu_freq: -1,
    gpu_usage: -1,
    ddr_freq: -1,

    temp_shell_frame: -1,

    power_current: -1,
    power_voltage: -1,

    net_up: -1,
    net_down: -1,
};

function build_period_data_cpu_map(para, index, report_detail) {
    if (!para.startsWith('cpu'))
        return;

    let cpu_index = parseInt(para[3]);

    if (para.endsWith('Load')) {
        if (cpu_index < report_detail.cpu_core_num) {
            period_data_map.cpu_usage[cpu_index] = index;
        }
    }

    if (!para.endsWith('Frequency')) {
        return;
    }

    let cluster_num = -1;
    for (let i = 0; i < report_detail.cpu_cluster_num; i++) {
        for (let j = 0; j < report_detail.cpu_index[i].length; j++) {
            if (report_detail.cpu_index[i][j] === cpu_index) {
                cluster_num = i;
                break;
            }
        }

        if (cluster_num !== -1)
            break;
    }

    if (cluster_num === -1) {
        return;
    }


    if (period_data_map.cpu_cluster_freq[cluster_num] === -1) {
        period_data_map.cpu_cluster_freq[cluster_num] = index;
    }
}

function build_period_data_map(line, report_detail) {
    period_data_map.cpu_cluster_freq = [];
    for (let i = 0; i < report_detail.cpu_cluster_num; i++) {
        period_data_map.cpu_cluster_freq.push(-1);
    }

    period_data_map.cpu_usage = [];
    for (let i = 0; i < report_detail.cpu_core_num; i++) {
        period_data_map.cpu_usage.push(-1);
    }

    let paras = line.split(',');
    for (let i = 0; i < paras.length; i++) {
        build_period_data_cpu_map(paras[i], i, report_detail);

        if (paras[i] === 'fps')
            period_data_map.fps = i;

        if (paras[i] === 'fpsJitters')
            period_data_map.fps_frame_time = i;

        if (paras[i] === 'gpuFrequency')
            period_data_map.gpu_freq = i;

        if (paras[i] === 'gpuLoad')
            period_data_map.gpu_usage = i;

        if (paras[i] === 'ddrFrequency')
            period_data_map.ddr_freq = i;

        if (paras[i] === 'shellFrameTemp')
            period_data_map.temp_shell_frame = i;

        if (paras[i] === 'currentNow')
            period_data_map.power_current = i;

        if (paras[i] === 'voltageNow')
            period_data_map.power_voltage = i;

        if (paras[i] === 'networkUpSpeed')
            period_data_map.net_up = i;

        if (paras[i] === 'networkDownSpeed')
            period_data_map.net_down = i;
    }
}

function process_data_after_reading(report_detail) {
    report_detail.fps_average /= report_detail.data_block_num;

    for (let i = 0; i < report_detail.cpu_cluster_num; i++) {
        let cpu_index_list = report_detail.cpu_index[i];

        for (let j = 0; j < cpu_index_list.length; j++) {
            report_detail.cpu_average_usage[cpu_index_list[j]] /= report_detail.data_block_num;
        }
    }
}

function parse_report_data(line, report_detail) {
    let values = line.split(',');
    if (line.length === 0)
        return;

    if (period_data_map.fps !== -1) {
        report_detail.fps.push(parseInt(values[period_data_map.fps]));
        report_detail.fps_average += parseInt(values[period_data_map.fps]);
    }

    for (let i = 0; i < report_detail.cpu_cluster_num; i++) {
        if (period_data_map.cpu_cluster_freq[i] !== -1) {
            report_detail.cpu_freq[i].push(parseInt(values[period_data_map.cpu_cluster_freq[i]]) / 1000);
        }

        let cpu_index_list = report_detail.cpu_index[i];
        for (let j = 0; j < cpu_index_list.length; j++) {
            if (period_data_map.cpu_usage[i] === -1) {
                continue;
            }

            let usage = parseFloat(values[period_data_map.cpu_usage[cpu_index_list[j]]]);
            report_detail.cpu_usage[cpu_index_list[j]].push(usage);
            report_detail.cpu_average_usage[cpu_index_list[j]] += usage;
        }
    }
}

export function openharmony_get_report_detail_info(path, report_detail) {
    const { start_time, duration } = openharmony_get_report_show_info(path);
    report_detail.start_time = start_time;

    generate_temp_info_for_report(report_detail);

    let period_data_file = path + '/t_index_info.csv';
    const period_data = fs.readFileSync(period_data_file, 'utf-8');
    let lines = period_data.split(/[(\r\n)\r\n]/);
    build_period_data_map(lines[0], report_detail);

    report_detail.data_block_num = lines.length - 2;
    for (let i = 1; i < lines.length; i++) {
        parse_report_data(lines[i], report_detail);
    }

    process_data_after_reading(report_detail);

}